#include"DList.h"
DList* DListFind(DList* pphead, TLDataType x)
{
	DList* pcur = pphead->next;
	while (pcur != pphead)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}


void DListPrint(DList* pphead)
{
	DList* pcur = pphead->next;
	while (pcur != pphead)
	{
		printf("%d ", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}
DList* BuyNode(TLDataType x)
{
	DList* node = (DList*)malloc(sizeof(DList));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	node->data = x;
	node->next = node->prev = node;
	return node;
}



void DListInit(DList** pphead)
{
	*pphead = BuyNode(-1);
}

void DListPushBack(DList* pphead, TLDataType x)
{
	assert(pphead);
	DList* newnode = BuyNode(x);
	newnode->prev = pphead->prev;
	newnode->next = pphead;

	pphead->prev->next = newnode;
	pphead->prev = newnode;
}

void DListPushFront(DList* pphead, TLDataType x)
{
	assert(pphead);

	DList* newnode = BuyNode(x);
	newnode->next = pphead->next;
	newnode->prev = pphead;
	pphead->next->prev = newnode;
	pphead->next = newnode;
}


void DListPopBack(DList* pphead)
{
	assert(pphead && pphead->next!=NULL);
	DList* del = pphead->prev;
	del->prev->next = pphead;
	pphead->prev = del->prev;

	free(del);
	del == NULL;

}


void DListPopFront(DList* pphead)
{
	assert(pphead && pphead->next != NULL);
	DList* del = pphead->next;
	pphead->next = del->next;
	del->next->prev = pphead;
	free(del);
	del = NULL;

}


void DListPushPosBack(DList*pos, TLDataType x)
{
	assert(pos);

	DList* newnode = BuyNode(x);
	//pos newnode pos->next
	newnode->next = pos->next;
	newnode->prev = pos;

	pos->next->prev = newnode;
	pos->next = newnode;
}

void DListPopPosBack(DList* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
	pos == NULL;
}


void DListDestory(DList* pphead)
{
	assert(pphead);
	DList* pcur = pphead->next;
	while (pcur != pphead)
	{
		DList*next = pcur->next;
		free(pcur);
		pcur = next;
	}
	free(pphead);
	pphead = NULL;

}


